<template>
  <el-card>
    <div class="table">
      <!-- 表格 -->
      <el-table v-loading="loading" :data="activityRequestList" style="width: 100%">
        <el-table-column v-for="item in activityTableLabel" :key="item.prop" :label="item.label" :prop="item.prop"
          :width="item.width" :sortable="item.sortable" />

        <!-- 活动状态 -->
        <el-table-column :filters="[{ text: '已通过', value: 1 }, { text: '申请中', value: 0 }, { text: '不通过', value: 0 }]"
          prop="status" label="活动状态" :filter-method="filterTag" width="95">
          <template v-slot="scope">
            <el-tag size="small" v-if="scope.row.status === 0" type="warning">申请中</el-tag>
            <el-tag size="small" v-else-if="scope.row.status === 1" type="success">已通过</el-tag>
            <el-tag size="small" v-else-if="scope.row.status === 2" type="danger">不通过</el-tag>
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column fixed="right" label="操作" align="center">
          <template v-slot="scope">
            <el-button type="primary" size="small" @click="showDetail(scope.$index)">
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination background layout="prev, pager, next" :total="config.total" class="pager mt-4"
        @current-change="changePage" :page-size="config.size" />
    </div>
  </el-card>

  <!-- 查看申请信息的模态框 -->
  <el-dialog v-model="dialogVisible" title="商品上架申请详情" width="50%">

    <!-- 商品详细信息展示表格 -->
    <el-descriptions :column="1" size="default" border>
      <el-descriptions-item label="参加活动请求ID" label-align="center" align="center">
        {{ participateRequestDetail.participateRequest.storeParticipateInActivityRequestId }}
      </el-descriptions-item>
      <el-descriptions-item label="补贴活动ID" label-align="center" align="center">
        {{ participateRequestDetail.participateRequest.activityId }}
      </el-descriptions-item>
      <el-descriptions-item label="商店ID" label-align="center" align="center">
        {{ participateRequestDetail.participateRequest.storeId }}
      </el-descriptions-item>
      <el-descriptions-item label="申请发起时间" label-align="center" align="center">
        {{ participateRequestDetail.participateRequest.initiateTime }}
      </el-descriptions-item>
      <el-descriptions-item label="申请审核时间" label-align="center" align="center">
        {{ participateRequestDetail.participateRequest.processTime }}
      </el-descriptions-item>
      <el-descriptions-item label="申请状态" label-align="center" align="center">
        <el-select v-model="participateRequestDetail.participateRequest.status" placeholder="Select">
          <el-option label="申请中" :value="0" :disabled="true" />
          <el-option label="通过" :value="1" />
          <el-option label="不通过" :value="2" />
        </el-select>
      </el-descriptions-item>
      <el-descriptions-item label="审核意见" label-align="center">
        <el-input v-model="participateRequestDetail.participateRequest.info" type="textarea" placeholder="请输入审核意见" />
      </el-descriptions-item>
    </el-descriptions>

    <!-- 底部的按钮 -->
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">
          取消
        </el-button>
        <el-button :disabled="originStatus !== 0" type="primary" @click="handleProcessRequest()">
          保存审核
        </el-button>
      </span>
    </template>

  </el-dialog>
</template>

<script setup>
import { getCurrentInstance, onMounted, ref, reactive } from 'vue';

const { proxy } = getCurrentInstance();
const activityRequestList = ref([]);
const loading = ref(false);  // 控制表格加载遮罩窗是否出现
const dialogVisible = ref(false);
const activityTableLabel = reactive([
  {
    prop: "storeParticipateInActivityRequestId",
    label: "参加活动请求ID",
    // width: 180,
    sortable: true,
  },
  {
    prop: "activityId",
    label: "活动ID",
    // width: 180,
    sortable: true,
  },
  {
    prop: "storeId",
    label: "商店ID",
    // width: 180,
    sortable: true,
  },
  {
    prop: "initiateTime",
    label: "发起时间",
    // width: 180,
    sortable: true,
  },
  {
    prop: "processTime",
    label: "处理时间",
    // width: 180,
    sortable: true,
  },
]);

let config = reactive({
  total: 0,  // 总的条目数
  current: 1,  // 当前页
  size: 15  // 一页的条目数
})

let participateRequestDetail = reactive({
  participateRequest: {
    "storeParticipateInActivityRequestId": "",
    "activityId": "",
    "storeId": "",
    "initiateTime": "",
    "processTime": "",
    "status": 0,
    "info": ""
  }
});

// 当前查看详情对应索引号
const currentIndex = ref(0);
// 原本的申请状态
const originStatus = ref(0);

/**
 * 获取活动列表的一页
 */
const getActivityRequestList = async () => {
  // 发起请求
  let res = await proxy.$api.activity.getPageOfParticipateRequest({
    current: config.current,
    size: config.size,
  });
  // 更新表格数据
  activityRequestList.value = res.data.activityRequestList;
  config.total = res.data.total;
}

/**
 * 翻页
 * @param {number} page 目标页码
 */
const changePage = async (page) => {
  loading.value = true;
  // 更新当前页码
  config.current = page
  await getActivityRequestList()
  loading.value = false;
}

let filterTag = (value, row) => {
  return row.status === value;
}

/**
 * 显示申请详情，便于管理员审核处理
 */
const showDetail = (index) => {
  // 更新详情对应索引
  currentIndex.value = index;
  // 更新原初状态
  originStatus.value = activityRequestList.value[index].status;
  participateRequestDetail.participateRequest = activityRequestList.value[index];
  dialogVisible.value = true;
}

/**
 * 请求审核完毕，保存审核结果
 */
const handleProcessRequest = async () => {
  let res = await ElMessageBox.confirm(
    '确认后将无法更改，确定保存该申请审核吗？',
    '确认审核',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    });
  if (res === 'confirm') {
    let ret = await proxy.$api.activity.processApplication(participateRequestDetail.participateRequest);
    if (ret.status === 200) {
      ElMessage.success({ showClose: true, message: "审核成功" });
    }
    dialogVisible.value = false;

    loading.value = true;
    await getActivityRequestList();
    loading.value = false;
  }
}

onMounted(async () => {
  loading.value = true;
  await getActivityRequestList();
  loading.value = false;
});
</script>

<style lang="less" scoped>
.table {
  // position: relative;

  .pager {
    position: relative;
    bottom: -15px;
    justify-content: flex-end;
    margin-right: 15px;
    margin-top: 10px;
    margin-bottom: 15px;
  }
}
</style>